<?php
#[内容管理]
$sysnav = "cate";

#[引入无限级分类]
require_once("class/unlimited_category.class.php");
$CT = new Category();
$r_url = $mainlink."?file=msg";

#[信息录入]
if($sysAct == "add")
{
	if(!$sysgroupid && !$cateid)
	{
		Error("操作不正确，系统无法获取相关信息...",$mainlink."?file=sysgroup&act=list");
	}
	if($cateid)
	{
		$rscate = $DB->qgGetOne("SELECT sysgroupid FROM ".$prefix."category WHERE id='".$cateid."'");
		if(!$rscate)
		{
			Error("分类ID不正确！",$mainlink."?file=sysgroup&act=list");
		}
		$sysgroupid = $rscate["sysgroupid"];
	}
	#[判断权限]
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	$group = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'");
	$rslist = $DB->qgGetAll("SELECT id,catename,catename_en,catename_ft,rootid,parentid FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' ORDER BY taxis ASC");
	$catelist = $chaincatelist = $CT->arraySet($rslist,0);
	unset($rslist);
	
	#[获取字段]
	if($proID)
	{
		$fieldlist = $PA->fieldlist($proID,"",false); //只读取字段列表不读取数据
	}
	else
	{
		if(!$cateid)
		{
			#[如果不存在字段方案，则显示全部字段]
			$ifcheck = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup_fieldgroup WHERE sysgroupid='".$sysgroupid."'");
			if(!$ifcheck)
			{
				$fircate = $DB->qgGetOne("SELECT id FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND status=1 ORDER BY taxis ASC,id ASC");
				if($fircate)
				{
					$fieldlist = $PA->fieldlist("",$fircate["id"]);
				}
			}
		}
		else
		{
			$fieldlist = $PA->fieldlist("",$cateid);
		}
	}
	
	Foot("msg.view.qg");
}
elseif($sysAct == "modify")
{
	$id = intval($id);
	if(!$id || $id == 0)
	{
		Error("操作不正确，无法取得ID号信息...",$mainlink."?file=sysgroup&act=list");
	}
	$rs = $DB->qgGetOne("SELECT * FROM ".$prefix."msg WHERE id='".$id."'");
	$rsC = $DB->qgGetOne("SELECT * FROM ".$prefix."msg_content WHERE id='".$id."'");
	$rsd = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$rs["thumb"]."'");	#[缩略图]
	$rsd2 = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$rs["thumb2"]."'");	#[缩略图2]
	$rsd3 = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$rs["thumb3"]."'");	#[缩略图3]
	$content = FckToHtml($rsC["content"]);
	$content_en = FckToHtml($rsC["content_en"]);
	$content_ft = FckToHtml($rsC["content_ft"]);
	$cateid = $rs["cateid"];
	$rsG = $DB->qgGetOne("SELECT sysgroupid FROM ".$prefix."category WHERE id='".$cateid."'");
	if(!$rsG)
	{
		Error("分类ID不正确！",$mainlink."?file=sysgroup&act=list");
	}
	$sysgroupid = $rsG["sysgroupid"];
	#[判断权限]
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	$group = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'");
	$rslist = $DB->qgGetAll("SELECT id,catename,catename_en,catename_ft,rootid,parentid FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."' ORDER BY `taxis` ASC");
	$catelist =$chaincatelist = $CT->arraySet($rslist,0);
	
	#[关联信息]
	if($rs["chainid"])
	{
		$rslist = explode(",",$rs["chainid"]);
		foreach($rslist AS $key=>$value)
		{
			$rsa = $DB->qgGetOne("SELECT m.*,u.folder,u.filename,u.thumbfile,u.markfile FROM ".$prefix."msg AS m LEFT JOIN ".$prefix."upfiles AS u ON m.thumb=u.id WHERE m.id='".$value."'");
			if($rsa)
			{
				$chainlist[] = $rsa;
			}
		}
	}
	
	#[获取字段]
	$fieldlist = $PA->fieldlist($id);
	
	Foot("msg.view.qg");
}
elseif($sysAct == "viewok")
{
	#[判断权限]
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	
	$id = intval($id);
	if($id)
	{
		$react = "modify";
	}
	else
	{
		$react= "add";
	}
	$cateid = $updata["cateid"] = intval($cateid);
	if(!$cateid)
	{
		Error("分类不允许为空...",$mainlink."?file=msg&act=".$react."&cateid=".$cateid);
	}
	$sysgroupid = intval($sysgroupid);
	$additonid = $updata["additonid"] = intval($additonid);
	#[读取系统组图片尺寸]
	if($sysgroupid)
	{
		$picsize = picsize($sysgroupid);
	}
	if($subject)
	{
		$updata["subject"] = $STR->safe($subject);
	}
	if($subject_en)
	{
		$updata["subject_en"] = $STR->safe($subject_en);
	}
	if($subject_ft)
	{
		$updata["subject_ft"] = $STR->safe($subject_ft);
	}
	if(!$updata["subject"] && !$updata["subject_en"] && !$updata["subject_ft"])
	{
		Error("主题不允许为空...",$mainlink."?file=msg&act=".$react."&cateid=".$cateid);
	}
	$updata["ifcheck"] = intval($ifcheck);#[审核状态]
	$updata["isrecommend"] = intval($isrecommend);#[首页推荐]
	$update["author"] = $author ? $author : $_SESSION["admin"]["user"];
	$update["author"] = $STR->safe($update["author"]);
	#[置顶、推荐、精华]
	$updata["isvouch"] = isset($isvouch) ? 1 : 0;
	$updata["istop"] = isset($istop) ? 1 : 0;
	$updata["isbest"] = isset($isbest) ? 1 : 0;
	$updata["levelid"] = intval($levelid);#[会员级别号]
	$updata["tplfile"] = $STR->safe($tplfile);#[自定义模板]
	#[主题标签]
	$tags = $STR->safe($tags);
	$updata["tags"] = str_replace(array("，"),array(","),$tags);
	#[缩略图ID号]
	$updata["thumb"] = intval($thumb);
	$updata["thumb2"] = intval($thumb2);
	$updata["thumb3"] = intval($thumb3);
	#[附件ID号]
	$updata["ufile"] = intval($ufile);
	$updata["postdate"] = $updata["orderdate"] = $postdate ? strtotime($postdate) : $system_time;#[发布时间]
	$updata["modifydate"] = $system_time;#[修改时间，使用系统时间]
	$updata["hits"] = intval($hits);
	#[SEO]
	$updata["meta_title"] = $STR->safe($meta_title);
	$updata["meta_keywords"] = $STR->safe($meta_keywords);
	$updata["meta_description"] = $STR->safe($meta_description);
	$updata["meta_title_en"] = $STR->safe($meta_title_en);
	$updata["meta_keywords_en"] = $STR->safe($meta_keywords_en);
	$updata["meta_description_en"] = $STR->safe($meta_description_en);
	$updata["meta_title_ft"] = $STR->safe($meta_title_ft);
	$updata["meta_keywords_ft"] = $STR->safe($meta_keywords_ft);
	$updata["meta_description_ft"] = $STR->safe($meta_description_ft);
	#[关联信息]
	$updata["chainid"] = "";
	if($chainid)
	{
		
		foreach($chainid AS $key=>$value)
		{
			$updata["chainid"] .= $value;
			if($key != count($chainid)-1)
			{
				$updata["chainid"] .= ",";
			}
		}
	}
	
	#[编辑]
	if($react=="modify")
	{
		$where["id"] = $id;
		#[写入msg表]
		$DB->update("msg",$updata,$where);
		unset($updata);
		
		#[写入msg_content表]
		$updata["cateid"] = $cateid;
		$updata["content"] = $content = $STR->html($content);
		$updata["content_en"] = $content_en = $STR->html($content_en);
		$updata["content_ft"] = $content_ft = $STR->html($content_ft);
		
		$check = $DB->qgGetOne("SELECT * FROM ".$prefix."msg_content WHERE id='".$id."'");
		if(!$check)
		{
			$DB->insert("msg_content",array("id"=>$id));
		}
		$DB->update("msg_content",$updata,$where);
		unset($updata,$where);
		
		#[写入msg_field表]
		$isfield = intval($isfield);
		if($isfield)
		{
			#[字符类型]
			if($field_text)
			{
				$field_text = $STR->safe($field_text);
				foreach($field_text AS $key=>$value)
				{
					if($key=="cn"){
						$fbi = "";
					}else{
						$fbi = "_".$key;
					}
					foreach($value AS $key2=>$value2)
					{
						$rsf = $DB->qgGetOne("SELECT id,sign,field_type FROM ".$prefix."sysgroup_field WHERE sign='".$key2."' AND sysgroupid='".$sysgroupid."'");
						$updata["field_content".$fbi] = $value2;
						if($rsf["field_type"]=="editor")
						{
							$updata["field_content".$fbi] = FckToHtml($updata["field_content".$fbi]);
						}
						$check = $DB->qgGetOne("SELECT * FROM ".$prefix."msg_field WHERE msgid='".$id."' AND field_sign='".$rsf["sign"]."'");
						if($check){
							$DB->update("msg_field",$updata,array("id"=>$check["id"]));
						}
						else{
							if($value2)
							{
								$updata["msgid"] = $id;
								$updata["fieldid"] = $rsf["id"];
								$updata["field_sign"] = $rsf["sign"];
								$DB->insert("msg_field",$updata);
							}
						}
						unset($updata);
					}
				}
			}
		}
	}
	#[添加]
	else
	{
		#[写入msg表]
		$update["postdate"] = $update["orderdate"] = $system_time;
		$insertid = $DB->insert("msg",$updata);
		unset($updata);
		
		#[写入msg_content表]
		$updata["id"] = $insertid;
		$updata["cateid"] = $cateid;
		$content = $updata["content"] = $STR->html($content);
		$content_en = $updata["content_en"] = $STR->html($content_en);
		$content_ft = $updata["content_ft"] = $STR->html($content_ft);
		$DB->insert("msg_content",$updata);
		unset($updata);
		
		#[写入msg_field表]
		$isfield = intval($isfield);
		if($isfield)
		{
			#[字符类型]
			if($field_text)
			{
				$field_text = $STR->safe($field_text);
				foreach($field_text AS $key=>$value)
				{
					if($key=="cn"){
						$fbi = "";
					}else{
						$fbi = "_".$key;
					}
					foreach($value AS $key2=>$value2)
					{
						$rsf = $DB->qgGetOne("SELECT id,sign,field_type FROM ".$prefix."sysgroup_field WHERE sign='".$key2."' AND sysgroupid='".$sysgroupid."'");
						$updata["field_content".$fbi] = $value2;
						if($rsf["field_type"]=="editor")
						{
							$updata["field_content".$fbi] = FckToHtml($updata["field_content".$fbi]);
						}
						$check = $DB->qgGetOne("SELECT * FROM ".$prefix."msg_field WHERE msgid='".$insertid."' AND field_sign='".$rsf["sign"]."'");
						if($check){
							$DB->update("msg_field",$updata,array("id"=>$check["id"]));
						}
						else{
							if($value2)
							{
								$updata["msgid"] = $insertid;
								$updata["fieldid"] = $rsf["id"];
								$updata["field_sign"] = $rsf["sign"];
								$DB->insert("msg_field",$updata);
							}
						}
						unset($updata);
					}
				}
			}
		}
	}
	
	#[把编辑器保存的图片存入数据库里]
	$content_total .= $content.$content_en.$content_ft;
	$imglist = editor_img($content_total);
	if($imglist)
	{
		foreach($imglist AS $key=>$value)
		{
			$imgarray = explode("/",$value);
			$folder = $imgarray[0]."/".$imgarray[1]."/".$imgarray[2]."/";
			$sql = "SELECT * FROM ".$prefix."upfiles WHERE folder='".$folder."' AND filename='".$imgarray[3]."'";
			$check = $DB->qgGetOne($sql);
			if(!$check)
			{
				$filename = $imgarray[3];
				if(is_file($folder.$filename))
				{
					#[获取当前服务器信息]
					$mypath = $UP->getpath();
					#[生成缩略图]
					$thumbfile = $GD->thumb($mypath.$filename);
					#[生成水印图]
					$markfile = $GD->mark($mypath.$filename,$picsize["width"],$picsize["height"]);
					$filetype = $UP->qgfiletype($imgarray[3]);
					$sql = "INSERT INTO ".$prefix."upfiles(filetype,tmpname,filename,folder,postdate,thumbfile,markfile) VALUES('".$filetype."','".$filename."','".$filename."','".$folder."','".$system_time."','".$thumbfile."','".$markfile."')";
					$DB->qgQuery($sql);
					unset($imgarray,$sql,$check,$farray);
				}
			}
		}
	}
	
	#[自动排序]
	$sqlorder = "SELECT id FROM ".$prefix."msg WHERE cateid=".$cateid." AND additonid=0 ORDER BY sortid ASC,id DESC";
	$allprolist = $DB->qgGetAll($sqlorder);
	$i = 1;
	foreach($allprolist AS $key=>$value)
	{
		$sql2 = "UPDATE ".$prefix."msg SET sortid=".$i." WHERE id=".$value["id"]."";
		$DB->qgQuery($sql2);
		$i++;
	}
	
	if($additonid) 
	{
		Error("信息编辑成功...",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid."&proID=".$additonid);
	}
	else
	{
		Error("信息编辑成功...",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
	}
}
elseif($sysAct == "field")
{
	$sysgroupid = intval($sysgroupid);
	
	$group = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'");
	if(!$sysgroupid)
	{
		Error("操作非法",$mainlink."?file=sysgroup&act=list");
	}
	
	#[字段方案]
	$field_count = $DB->qg_count("SELECT count(*) FROM ".$prefix."sysgroup_field WHERE sysgroupid='".$sysgroupid."' AND ifcheck=1");
	if($field_count>0)
	{
		$fieldgroup = $DB->qgGetAll("SELECT * FROM ".$prefix."sysgroup_fieldgroup WHERE sysgroupid='".$sysgroupid."' ORDER BY id ASC");
	}
	$rs = $DB->qgGetOne("SELECT * FROM ".$prefix."msg WHERE id='".$id."'");
	$rs = lanmerge($rs,"subject");
	$rsu = get_thumbarr($rs["thumb"]);
	$rsc = $DB->qgGetOne("SELECT * FROM ".$prefix."category WHERE id='".$rs["cateid"]."'");
	$rsc = lanmerge($rsc,"catename");
	FOOT("msg.field.qg");
}
elseif($sysAct == "fieldok")
{
	$updata["fieldgroupid"] = intval($fieldgroupid);
	$DB->update("msg",$updata,array("id"=>$id));
	unset($updata);
	Error("字段方案关联成功",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
}
elseif($sysAct == "plset")
{
	$cateid = intval($cateid);
	$sysgroupid = intval($sysgroupid);
	#[判断权限]
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	$myidlist = $STR->safe($idlist);#[获取IDlist]
	if(!$myidlist)
	{
		Error("信息操作不正确",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid);
	}
	$qgtype = $STR->safe($qgtype);
	if($qgtype == "delete")
	{
	
		$proID = $STR->safe($proID);
		$idarray = explode(",",$myidlist);
		foreach($idarray AS $key=>$value)
		{
			
			$value = intval($value);
			if($value)
			{
				$query = "SELECT * FROM ".$prefix."msg WHERE additonid='".$value."' ";
				$result=mysql_query($query);
				$number=mysql_num_rows($result);
				if ($number > 0 )
				{

					Error("请先删除主题下的附加内容！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
				}
				#[检测标签数]
				$msgRs = $DB->qgGetOne("SELECT * FROM ".$prefix."msg WHERE id='".$value."'");
				$cateid = $msgRs["cateid"];
				$DB->qgQuery("DELETE FROM ".$prefix."msg WHERE id='".$value."'");#[删除主题]
				$DB->qgQuery("DELETE FROM ".$prefix."msg_content WHERE id='".$value."'");#[删除内容]
				#[检测自定义字段]
				$check = $DB->qgGetAll("SELECT * FROM ".$prefix."msg_field WHERE msgid='".$value."'");
				if($check)
				{
					$DB->qgQuery("DELETE FROM ".$prefix."msg_field WHERE msgid='".$value."'");#[删除字段]
				}
			}
		}
		if($proID) 
		{
			Error("删除主题完成！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&proID=".$proID."&cateid=".$cateid);
		}
		else
		{
			Error("删除主题完成！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
		}
	}
	elseif($qgtype == "deletepic")
	{
	
		$proID = $STR->safe($proID);
		$idarray = explode(",",$myidlist);
		foreach($idarray AS $key=>$value)
		{
			
			$value = intval($value);
			if($value)
			{
				$query = "SELECT * FROM ".$prefix."msg WHERE additonid='".$value."' ";
				$result=mysql_query($query);
				$number=mysql_num_rows($result);
				if ($number > 0 )
				{

					Error("请先删除主题下的附加内容！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
				}
				#[检测标签数]
				$msgRs = $DB->qgGetOne("SELECT * FROM ".$prefix."msg WHERE id='".$value."'");
				$cateid = $msgRs["cateid"];
				$DB->qgQuery("DELETE FROM ".$prefix."msg WHERE id='".$value."'");#[删除主题]
				$DB->qgQuery("DELETE FROM ".$prefix."msg_content WHERE id='".$value."'");#[删除内容]
				#[检测自定义字段]
				$check = $DB->qgGetAll("SELECT * FROM ".$prefix."msg_field WHERE msgid='".$value."'");
				if($check)
				{
					$DB->qgQuery("DELETE FROM ".$prefix."msg_field WHERE msgid='".$value."'");#[删除字段]
				}
				
				#[图片]
				$msgpic = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$msgRs["thumb"]."'");
				if($msgpic["filename"])
				{
					$FS->qgDelete($msgpic["folder"].$msgpic["thumbfile"]);
					$FS->qgDelete($msgpic["folder"].$msgpic["markfile"]);
					$FS->qgDelete($msgpic["folder"].$msgpic["filename"]);
				}
				$DB->qgQuery("DELETE FROM ".$prefix."upfiles WHERE id='".$msgRs["thumb"]."'");
			}
		}
		if($proID) 
		{
			Error("彻底删除主题完成！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&proID=".$proID."&cateid=".$cateid);
		}
		else
		{
			Error("彻底删除主题完成！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
		}
	}
	else if($qgtype == "move")
	{
		$idarray = explode(",",$myidlist);
		foreach($idarray AS $key=>$value)
		{
			#[移动主题]
			$DB->qgQuery("UPDATE ".$prefix."msg SET cateid=".$moveid." WHERE id='".$value."'");
			#[移动内容]
			$check = $DB->qgGetOne("SELECT * FROM ".$prefix."msg_content WHERE id='".$value."'");
			if($check)
			{
				$DB->qgQuery("UPDATE ".$prefix."msg_content SET cateid=".$moveid." WHERE id='".$value."'");
			}
			#[移动相册]
			$check = $DB->qgGetAll("SELECT * FROM ".$prefix."msg WHERE additonid='".$value."'");
			if($check)
			{
				$DB->qgQuery("UPDATE ".$prefix."msg SET cateid=".$moveid." WHERE additonid='".$value."'");
			}
		}
		Error("移动主题完成！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
	}
	else if($qgtype == "additon")
	{
		$myidlist_s = explode(',',$myidlist);
		if(in_array($additonid,$myidlist_s))
		{
			Error("移动到相册不能包含自己！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
		}
		else
		{
			$DB->qgQuery("UPDATE ".$prefix."msg SET additonid=".$additonid." WHERE id in(".$myidlist.")");#[移动到相册]
			Error("移动到相册完成！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
		}
	}
	else
	{
		$sql = "UPDATE ".$prefix."msg SET ";
		switch ($qgtype)
		{
			case "top":
				$sql .= "istop='1'";
				$tmsg = "置顶";
			break;
			case "vouch":
				$sql .= "isvouch='1'";
				$tmsg = "新品";
			break;
			case "recommend":
				$sql .= "isrecommend='1'";
				$tmsg = "首页推荐";
			break;
			case "best":
				$sql .= "isbest='1'";
				$tmsg = "精华";
			break;
			case "dtop":
				$sql .= "istop='0'";
				$tmsg = "取消置顶";
			break;
			case "dvouch":
				$sql .= "isvouch='0'";
				$tmsg = "取消新品";
			break;
			case "drecommend":
				$sql .= "isrecommend='0'";
				$tmsg = "取消首页推荐";
			break;
			case "dbest":
				$sql .= "isbest='0'";
				$tmsg = "取消精华";
			break;
			case "check":
				$sql .= "ifcheck='1'";
				$tmsg = "审核";
			break;
			case "dcheck":
				$sql .= "ifcheck='0'";
				$tmsg = "未审核";
			break;
			case "orderdate":
				$sql .= "orderdate='".$system_time."'";
				$tmsg = "排序提前";
			break;
			default :
				$sql .= "isbest='1'";
				$tmsg = "精华";
			break;
		}
		$sql .= " WHERE id in(".$myidlist.")";
		$DB->qgQuery($sql);
		$proID = $STR->safe($proID);
		if($proID) 
		{
			Error("批量 <span style='color:red;'>".$tmsg."</span> 操作完成！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&proID=".$proID);
		}
		else
		{
			Error("批量 <span style='color:red;'>".$tmsg."</span> 操作完成！",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
		}
	}
}
elseif($sysAct == "orderlist")
{


	$sysgroupid = intval($sysgroupid);
	if(!$sysgroupid)
	{
		Error("操作非法",$mainlink."?file=sysgroup&act=list");
	}
	#[判断权限]
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	#[显示系统组名称]
	$sql = "SELECT groupname FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'";
	$rs = $DB->qgGetOne($sql);
	if(!$rs)
	{
		Error("找不到相关系统组",$mainlink."?file=sysgroup&act=list");
	}
	$groupname = $rs["groupname"];

	#[根据系统组显示名称]
	$sql = "SELECT id,catename,catename_en,catename_ft,rootid,parentid FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."' ORDER BY `taxis` ASC";
	$catelist = $DB->qgGetAll($sql);
	if(!$catelist || !is_array($catelist) || count($catelist)<1)
	{
		Error("暂无分类，请先添加分类....",$mainlink."?file=category&act=add&sysgroupid=".$sysgroupid);
	}
	#[得到临时分类组合，基于分类ID]
	foreach($catelist AS $key=>$value)
	{
		$cateid_array[$value["id"]] = $value["id"];
		$tmp_catename[$value["id"]] = $value["catename"];
	}
	
	$catelist = $CT->arraySet($catelist,0);#[这里的catelist仅用于显示select框]
	#[设置可选]
	$cateid = intval($cateid);
	#[得到cateid_array]
	if($cateid)
	{
		$cateid_array = array($cateid=>$cateid);
		$cateid_array = get_son_id_array($cateid,$catelist,$cateid_array);
	}
	/*
	if(count($cateid_array)<1)
	{
		Error("操作有错误",$mainlink."?file=sysgroup&act=list");
	}
	*/
	
	$condition = "WHERE m.cateid=c.id AND c.language='".$language."'";
	
	$sql33 = "SELECT * FROM ".$prefix."category AS c WHERE id ='".$cateid."'";
	$rslist33 = $DB->qgGetOne($sql33);



	if($otherid)
	{
		$condition .= " AND m.additonid =".$otherid." AND m.albumtype ='0' ";
	}

	if ($rslist33["parentid"]==0)
	{
		$condition .= " AND m.cateid IN(".implode(",",$cateid_array).")";
	}
	else
	{
	$condition .= " AND m.cateid IN(".$cateid.")";
	}
	$rs_cate = array();
	$condition .= " AND c.sysgroupid='".$sysgroupid."'";

	

	$sql = "SELECT m.id FROM ".$prefix."msg AS m,".$prefix."category AS c ".$condition." ORDER BY m.id DESC ";
	$rs_id = $DB->qgGetOne($sql);
	$get_id = $rs_id["id"];
	#$condition .= " AND m.id<='".$get_id."'";
$proID = $STR->safe($proID);
	if ($proID) 
	{	
		$sys_additionid = 0;
		$condition .= " AND m.additonid='".$proID."'";
	}
	else if(!$otherid)
	{
		$condition .= " AND m.additonid= 0 ";
	}
	if ($rslist33["parentid"]==0)
	{
		$cateid = 0;
	}

	if ($cateid ==0)
	{
	$sql = "SELECT m.id,m.cateid,m.subject,m.subject_en,m.author,m.postdate,m.modifydate,m.orderdate,m.thumb,m.ifcheck,m.istop,m.isvouch,m.isbest,m.hits,c.catename FROM ".$prefix."msg AS m,".$prefix."category AS c ".$condition." ORDER BY m.istop DESC,m.allsortid asc,m.id desc";
	}
	else
	{	
	$sql = "SELECT m.id,m.cateid,m.subject,m.subject_en,m.author,m.postdate,m.modifydate,m.orderdate,m.thumb,m.ifcheck,m.istop,m.isvouch,m.isbest,m.hits,c.catename FROM ".$prefix."msg AS m,".$prefix."category AS c ".$condition." ORDER BY m.istop DESC,m.sortid asc,m.id desc";
	}
	


	$rslist = $DB->qgGetAll($sql,true);
	foreach($rslist AS $key=>$value)
	{
		if(!$value["subject"] && $value["subject_en"])
		{
			$value["subject"] = $value["subject_en"];
		}
		if(!$value["subject"] && $value["subject_ft"])
		{
			$value["subject"] = $value["subject_ft"];
		}
		$value["subject_en"] = CutString($value["subject_en"],50);
		$msglist[] = $value;
	}
	Foot("msgorder.list.qg");
}
elseif($sysAct == "orderbyok")
{
	$i=0;
	$listlayer = substr($listlayer,0,(strlen($listlayer)-1));
	//print_r(explode("=",$listlayer));
	
	$sql = "SELECT *  FROM ".$prefix."category AS c WHERE id ='".$cateid."'";
	$rslist = $DB->qgGetOne($sql);
	if ($rslist["parentid"]==0)
	{
		$cateid = 0;
	}
	foreach(explode("=",$listlayer) as $updownlist)
	{
		$i=$i+1;
		if ($cateid ==0)
		{
			$sql="UPDATE ".$prefix."msg SET allsortid='".$i."' WHERE id='".$updownlist."'";
		}
		else
		{
			$sql="UPDATE ".$prefix."msg SET sortid='".$i."' WHERE id='".$updownlist."'";
		}
		$DB->qgQuery($sql);
	}
	$relink = $mainlink."?file=msg&act=orderlist&sysgroupid=".$sysgroupid."&cateid=".$cateid;
	if($otherid)
	{
		$relink .= "&otherid=".$otherid;
	}
	Error("排序成功...",$relink);
}
elseif($sysAct == "neworderlist")
{
	$sysgroupid = intval($sysgroupid);
	if(!$sysgroupid)
	{
		Error("操作非法",$mainlink."?file=sysgroup&act=list");
	}
	#[判断权限]
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	#[显示系统组名称]
	$sql = "SELECT groupname FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'";
	$rs = $DB->qgGetOne($sql);
	if(!$rs)
	{
		Error("找不到相关系统组",$mainlink."?file=sysgroup&act=list");
	}
	$groupname = $rs["groupname"];

	#[根据系统组显示名称]
	$sql = "SELECT id,catename,catename_en,catename_ft,rootid,parentid FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."' ORDER BY `taxis` ASC";
	$catelist = $DB->qgGetAll($sql);
	foreach($catelist AS $key=>$value)
	{
		if(!$value["catename"] && $value["catename_en"])
		{
			$value["catename"] = $value["catename_en"];
		}
		if(!$value["catename"] && $value["catename_ft"])
		{
			$value["catename"] = $value["catename_ft"];
		}
		$catelist[$key] = $value;
	}
	if(!$catelist || !is_array($catelist) || count($catelist)<1)
	{
		Error("暂无分类，请先添加分类....",$mainlink."?file=category&act=add&sysgroupid=".$sysgroupid);
	}
	#[得到临时分类组合，基于分类ID]
	foreach($catelist AS $key=>$value)
	{
		$cateid_array[$value["id"]] = $value["id"];
		$tmp_catename[$value["id"]] = $value["catename"];
	}
	
	$catelist = $CT->arraySet($catelist,0);#[这里的catelist仅用于显示select框]
	#[设置可选]
	$cateid = intval($cateid);
	#[得到cateid_array]
	if($cateid)
	{
		$cateid_array = array($cateid=>$cateid);
		$cateid_array = get_son_id_array($cateid,$catelist,$cateid_array);
	}
	/*
	if(count($cateid_array)<1)
	{
		Error("操作有错误",$mainlink."?file=sysgroup&act=list");
	}
	*/
	
	$condition = "WHERE m.cateid=c.id AND c.language='".$language."'";
	
	$sql33 = "SELECT * FROM ".$prefix."category AS c WHERE id ='".$cateid."'";
	$rslist33 = $DB->qgGetOne($sql33);



	if($otherid)
	{
		$condition .= " AND m.additonid =".$otherid;
	}

	if ($rslist33["parentid"]==0)
	{
		$condition .= " AND m.cateid IN(".implode(",",$cateid_array).")";
	}
	else
	{
	$condition .= " AND m.cateid IN(".$cateid.")";
	}
	$rs_cate = array();
	$condition .= " AND c.sysgroupid='".$sysgroupid."'";

	

	$sql = "SELECT m.id FROM ".$prefix."msg AS m,".$prefix."category AS c ".$condition." ORDER BY m.id DESC ";
	$rs_id = $DB->qgGetOne($sql);
	$get_id = $rs_id["id"];
	#$condition .= " AND m.id<='".$get_id."'";
$proID = $STR->safe($proID);
	if ($proID) 
	{	
		$sys_additionid = 0;
		$condition .= " AND m.additonid='".$proID."'";
	}
	else if(!$otherid)
	{
		$condition .= " AND m.additonid= 0 ";
	}
	if ($rslist33["parentid"]==0)
	{
		$cateid = 0;
	}

	if ($cateid ==0)
	{
	$sql = "SELECT m.id,m.cateid,m.subject,m.subject_en,m.author,m.postdate,m.modifydate,m.orderdate,m.thumb,m.ifcheck,m.istop,m.isvouch,m.isbest,m.hits,c.catename FROM ".$prefix."msg AS m,".$prefix."category AS c ".$condition." ORDER BY m.istop DESC,m.allsortid asc,m.id desc";
	}
	else
	{	
	$sql = "SELECT m.id,m.cateid,m.subject,m.subject_en,m.author,m.postdate,m.modifydate,m.orderdate,m.thumb,m.ifcheck,m.istop,m.isvouch,m.isbest,m.hits,c.catename FROM ".$prefix."msg AS m,".$prefix."category AS c ".$condition." ORDER BY m.istop DESC,m.sortid asc,m.id desc";
	}
	


	$rslist = $DB->qgGetAll($sql,true);
	$i = 1;
	foreach($rslist AS $key=>$value)
	{
		$rs = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$value["thumb"]."'");
		$value["folder"] = $rs["folder"];
		$value["filename"] = $rs["filename"];
		$value["thumbfile"] = $rs["thumbfile"];
		$value["markfile"] = $rs["markfile"];
		
		if(!$value["subject"] && $value["subject_en"])
		{
			$value["subject"] = $value["subject_en"];
		}
		if(!$value["subject"] && $value["subject_ft"])
		{
			$value["subject"] = $value["subject_ft"];
		}
		
		$value["row_index"] = $i;
		$i++;
		$msglist[] = $value;
	}
	Foot("newmsgorder.list.qg");
}
elseif($sysAct == "neworderbyok")
{
	if($allsortid)
	{
		$i = 1;
		foreach($allsortid AS $key=>$value)
		{
			$sql="UPDATE ".$prefix."msg SET allsortid='".$i."' WHERE id='".$value."'";
			$DB->qgQuery($sql);
			$i++;
		}
	}
	elseif($sortid)
	{
		$i = 1;
		foreach($sortid AS $key=>$value)
		{
			$sql="UPDATE ".$prefix."msg SET sortid='".$i."' WHERE id='".$value."'";
			$DB->qgQuery($sql);
			$i++;
		}
	}
	$relink = $mainlink."?file=msg&act=neworderlist&sysgroupid=".$sysgroupid."&cateid=".$cateid;
	if($otherid)
	{
		$relink .= "&otherid=".$otherid;
	}
	Error("排序成功...",$relink);
}
elseif($sysAct == "orderauto")
{
	#[根据系统组显示名称]
	$sql = "SELECT id,catename,catename_en,catename_ft,rootid,parentid FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."' ORDER BY `taxis` ASC";
	$catelist = $DB->qgGetAll($sql);
	if(!$catelist || !is_array($catelist) || count($catelist)<1)
	{
		Error("暂无分类，请先添加分类....",$mainlink."?file=category&act=add&sysgroupid=".$sysgroupid);
	}
	#[得到临时分类组合，基于分类ID]
	foreach($catelist AS $key=>$value)
	{
		$cateid_array[$value["id"]] = $value["id"];
		$tmp_catename[$value["id"]] = $value["catename"];
	}
	$catelist = $CT->arraySet($catelist,0);#[这里的catelist仅用于显示select框]
	#[设置可选]
	$cateid = intval($cateid);
	#[得到cateid_array]
	if($cateid)
	{
		$cateid_array = array($cateid=>$cateid);
		$cateid_array = get_son_id_array($cateid,$catelist,$cateid_array);
	}
	/********** sortid **********/
	foreach($cateid_array AS $key=>$value)
	{
		$i = 1;
		$sql = "SELECT * FROM ".$prefix."msg WHERE cateid=".$value." AND additonid=0 ORDER BY sortid asc,id desc";
		$msglist = $DB->qgGetAll($sql);
		foreach($msglist AS $key=>$value2)
		{
			$sql = "UPDATE ".$prefix."msg SET sortid=".$i." WHERE id=".$value2["id"];
			$DB->qgQuery($sql);
			$i++;
		}
	}
	/********** allsortid **********/
	$sql = "SELECT * FROM ".$prefix."msg WHERE cateid IN(".implode(",",$cateid_array).") AND additonid=0 ORDER BY allsortid asc,id desc";
	$msglist = $DB->qgGetAll($sql);
	$i = 1;
	foreach($msglist AS $key=>$value)
	{
		$sql = "UPDATE ".$prefix."msg SET allsortid=".$i." WHERE id=".$value["id"];
		$DB->qgQuery($sql);
		$i++;
	}
	Error("排序成功...",$mainlink."?file=msg&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid);
}
else
{
	$sysgroupid = intval($sysgroupid);
	
	$group = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'");
	if(!$sysgroupid)
	{
		Error("操作非法",$mainlink."?file=sysgroup&act=list");
	}
	#[判断权限]
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	#[显示系统组名称]
	$sql = "SELECT groupname FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'";
	$rs = $DB->qgGetOne($sql);
	if(!$rs)
	{
		Error("找不到相关系统组",$mainlink."?file=sysgroup&act=list");
	}
	$groupname = $rs["groupname"];

	#[根据系统组显示名称]
	$sql = "SELECT id,catename,catename_en,catename_ft,rootid,parentid FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."' ORDER BY `taxis` ASC";
	$catelist = $DB->qgGetAll($sql);
	foreach($catelist AS $key=>$value)
	{
		if(!$value["catename"] && $value["catename_en"])
		{
			$value["catename"] = $value["catename_en"];
		}
		if(!$value["catename"] && $value["catename_ft"])
		{
			$value["catename"] = $value["catename_ft"];
		}
		$catelist[$key] = $value;
	}
	if(!$catelist || !is_array($catelist) || count($catelist)<1)
	{
		Error("暂无分类，请先添加分类....",$mainlink."?file=category&act=add&sysgroupid=".$sysgroupid);
	}
	#[得到临时分类组合，基于分类ID]
	foreach($catelist AS $key=>$value)
	{
		$cateid_array[$value["id"]] = $value["id"];
		$tmp_catename[$value["id"]] = $value["catename"];
	}
	$catelist = $CT->arraySet($catelist,0);#[这里的catelist仅用于显示select框]
	#[设置可选]
	$cateid = intval($cateid);
	#[得到cateid_array]
	if($cateid)
	{
		$cateid_array = array($cateid=>$cateid);
		$cateid_array = get_son_id_array($cateid,$catelist,$cateid_array);
	}
	if(count($cateid_array)<1)
	{
		Error("操作有错误",$mainlink."?file=sysgroup&act=list");
	}
	//if ($picin!=1){$modifylist = " AND m.additonid=0";}
	$page_url = $r_url."&act=list&sysgroupid=".$sysgroupid."&cateid=".$cateid;

	$condition = "WHERE m.cateid=c.id AND c.language='".$language."'";
	$condition .= " AND m.cateid IN(".implode(",",$cateid_array).")";
	$condition .= " ".$modifylist;
	$rs_cate = array();
	$condition .= " AND c.sysgroupid='".$sysgroupid."'";
	
	

	#[获取关键字]
	$keywords = $STR->safe($keywords);
	$keywords = trim($keywords);
	if($keywords)
	{
		$keywords_array = explode(" ",$keywords);
		$keywords_count = count($keywords_array);
		if($keywords_count > 1)
		{
			$c_array = array();
			foreach($keywords_array AS $key=>$value)
			{
				$value = trim($value);
				if($value)
				{
					$value = str_replace("*","%",$value);
					$c_array[] = "(m.subject LIKE '%".$value."%' OR m.subject_en LIKE '%".$value."%' OR m.subject_ft LIKE '%".$value."%')";
				}
			}
			if(count($c_array)>0)
			{
				$condition .= " AND (".implode(" OR ",$c_array).")";
				$page_url .= "&keywords=".rawurlencode($keywords);
			}
		}
		elseif($keywords_count == 1)
		{
			$keywords = str_replace("*","%",$keywords);
			$condition .= " AND (m.subject LIKE '%".$keywords."%' OR m.subject_en LIKE '%".$keywords."%' OR m.subject_ft LIKE '%".$keywords."%')";
			$page_url .= "&keywords=".rawurlencode($keywords);
		}
		unset($keywords_count,$keywords_array);
	}
	#[获取时间段]
	$s_time = $STR->safe($s_time);
	$e_time = $STR->safe($e_time);
	if($s_time)
	{
		$start_time = strtotime($s_time);
		$condition .= " AND m.postdate>='".$start_time."'";
		$page_url .= "&s_time=".rawurlencode($s_time);
	}
	if($e_time)
	{
		$end_time = strtotime($e_time);
		$condition .= " AND m.postdate<='".$end_time."'";
		$page_url .= "&e_time=".rawurlencode($e_time);
	}
	#[获取是否审核]
	$ifcheck = intval($ifcheck);
	if($ifcheck)
	{


		if($ifcheck == 1)
		{
			$condition .= " AND m.ifcheck='1'";
			$page_url .= "&ifcheck=1";
		}
		else
		{
			$condition .= " AND m.ifcheck!=1";
			$page_url .= "&ifcheck=2";
		}
	}
	
	if ($show)
	{
		$condition .= " AND m.isrecommend=1";	
	}
	
	if($proID)
	{
		$page_url .= "&proID=".$proID;
	}
	if($proID) 
	{	
		$sys_additionid = 0;
		$condition .= " AND m.additonid='".$proID."' and m.albumtype='0' ";
	}
	else
	{
		$condition .= " AND m.additonid=0";
	}
	if($selfid)
	{
		$condition .= " AND m.id!='".$selfid."'";
	}
	if($act=="ajax")
	{
		$condition .= " AND m.ifcheck=1";
	}
	$psize = 30;
	$pageid = intval($pageid);
	$offset = $pageid>0 ? ($pageid-1)*$psize : 0;
	#[获取个数]
	$sql = "SELECT count(m.id) FROM ".$prefix."msg AS m,".$prefix."category AS c ".$condition;
	$msg_count = $DB->qg_count($sql);
	$pagelist = page($page_url,$msg_count,$psize,$pageid);#[获取页数信息]
	#[加条件]
	$sql = "SELECT m.id FROM ".$prefix."msg AS m,".$prefix."category AS c ".$condition." ORDER BY m.id DESC LIMIT ".$offset.",1";
	$rs_id = $DB->qgGetOne($sql);
	$get_id = $rs_id["id"];
	#$condition .= " AND m.id<='".$get_id."'";
	$proID = $STR->safe($proID);
	if($proID)
	{
		$sql2 = "SELECT * FROM ".$prefix."msg WHERE id=".$proID;
		$additon = $DB->qgGetOne($sql2,true);
		
		$sql2 = "SELECT * FROM ".$prefix."msg WHERE id=".$additon["additonid"];
		$upadditon = $DB->qgGetOne($sql2,true);
	}
	$rspar = $DB->qgGetOne("SELECT * FROM ".$prefix."category WHERE id='".$cateid."'");
	if($rspar["parentid"]==0)
	{
		$orderby = "m.istop DESC,m.allsortid asc,m.id desc";
	}
	else
	{	
		$orderby = "m.istop DESC,m.sortid asc,m.id desc";
	}
	$sql = "SELECT m.*,c.catename,c.catename_en,c.catename_ft FROM ".$prefix."msg AS m,".$prefix."category AS c ".$condition." ORDER BY ".$orderby." LIMIT ".$offset.",".$psize;
	$rslist = $DB->qgGetAll($sql,true);
	$i = 1;
	foreach($rslist AS $key=>$value)
	{
		if(!$value["subject"] && $value["subject_en"])
		{
			$value["subject"] = $value["subject_en"];
		}
		if(!$value["subject"] && $value["subject_ft"])
		{
			$value["subject"] = $value["subject_ft"];
		}
		if($value["subject"] == $value["subject_en"])
		{
			unset($value["subject_en"]);
		}
		if($value["subject"] == $value["subject_ft"])
		{
			unset($value["subject_ft"]);
		}
		
		#[读取缩略图]
		$sql8 = "SELECT * FROM ".$prefix."upfiles WHERE id='".$value["thumb"]."' ";
	    $rsc = $DB->qgGetOne($sql8);
		$value["folder"] = $rsc["folder"];
		$value["thumbfile"] = $rsc["thumbfile"];
		$value["filename"] = $rsc["filename"];
		$value["markfile"] = $rsc["markfile"];
		
		#[图片相册数量]
		$sql8 = "SELECT count(id) FROM ".$prefix."msg WHERE additonid='".$value["id"]."' and albumtype='0' ";
	    $value["count"] = $DB->qg_count($sql8);
		
		$value["postdate"] = date("Y-m-d",$value["postdate"]);
		
		#[判断content是否有内容]
		$sql10 = "SELECT * FROM ".$prefix."msg_content WHERE id=".$value["id"]." ";
		$rsc3 = $DB->qgGetOne($sql10);
		if($rsc3["content"] && !$rsc3["content_en"])
		{
			//$value["ifcontent"] = true;
		}
		
		$value["modifydate"] = date("Y-m-d",$value["modifydate"]);
		if(!$value["catename"] && $value["catename_en"])
		{
			$value["catename"] = $value["catename_en"];
		}
		if(!$value["catename"] && $value["catename_ft"])
		{
			$value["catename"] = $value["catename_ft"];
		}
	
		$value["row_index"] = $i;
		$i++;
		$msglist[] = $value;
	}
	
	#[字段方案]
	$field_count = $DB->qg_count("SELECT count(*) FROM ".$prefix."sysgroup_field WHERE sysgroupid='".$sysgroupid."' AND ifcheck=1");
	if($field_count>0)
	{
		$fieldgroup = $DB->qgGetAll("SELECT * FROM ".$prefix."sysgroup_fieldgroup WHERE sysgroupid='".$sysgroupid."' ORDER BY id ASC");
	}
	
	if($act=="ajax")
	{
		Foot("msg.ajax.qg");
	}
	else
	{
		Foot("msg.list.qg");
	}
}

function get_son_id_array($id,$array,$myid=array())
{
	foreach($array AS $key=>$value)
	{
		if($value["parentid"] == $id)
		{
			$myid[$value["id"]] = $value["id"];
			get_son_id_array($value["id"],$array,$myid);
		}
	}
	return $myid;
}
?>